﻿using Assets.Scripts.DL.Consts;
using Assets.Scripts.DL.Enemy.CommonEnemy.States.Special;
using Assets.Scripts.DL.Mgrs;
using System.Collections;
using UnityEngine;
using UnityEngine.UIElements;

namespace Assets.Scripts.DL.ComponentScripts {
	public class YsShadow : MonoBehaviour {
		[SerializeField] Animator animator;
		[SerializeField] int _Damage = 20;
		[SerializeField] SpriteRenderer sr;
		[SerializeField] float ScreennBrokenOff;
		float angle;//旋转角，角度制
		Vector3 v;

		bool _istouchPlayer;
		public void Init(int dir, Vector2 v, float angle, Vector3 pos) {
			transform.position = pos;
			transform.localScale = new Vector3(dir, 1, 1);
			if (dir < 0) {
				angle -= 180;
			}
			Vector3 currentEulerAngles = transform.eulerAngles;
			currentEulerAngles.z = angle;
			this.angle = angle;
			transform.eulerAngles = currentEulerAngles;
			_istouchPlayer = false;
			this.v = v;
			animator.Play("ready");
			SetAlpha(1);
		}

		private void Update() {
			transform.position += v * Time.deltaTime;
		}

		private void OnTriggerEnter2D(Collider2D collision) {
			if (!_istouchPlayer && collision.CompareTag(Tags.Player)) {
				_istouchPlayer = true;
				animator.Play("atk");
				collision.GetComponent<PlayerController>().TakeDamage(_Damage);
				//屏幕特效+震动
				ScreenEffectMgr.Instance.CutEffect(collision.transform.position, angle);
				var dir = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle)).normalized;
				CameraImpulseMgr.Instance.Generate(dir);
				StartCoroutine(Fade());
				v *= 2;
			}
		}

		IEnumerator Fade() {
			yield return new WaitForSeconds(0.3f);
			v = Vector3.zero;

			while (sr.color.a > 0.01) {
				SetAlpha(Mathf.Lerp(sr.color.a, 0, 0.02f));
				yield return null;
			}
			ObjectPool.Instance.RecycleObject(ObjPoolStr.YsShadow, this.gameObject);
		}
		void SetAlpha(float a) {
			var c = sr.color;
			c.a = a;
			sr.color = c;
		}
	}
}
